#!/usr/bin/perl -w
use strict;
use FindBin;
use Net::FTP;
use LWP::Simple;
use HTTP::Date;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin");

my ($cmd);

print STDERR <<"HEADLINE";
888888888888888888888888888888888888888888888888888888888888888888888

    table_assembly_gaps

	Description:
		Loads UCSC assembl data if present
	Populates DB tables:
		taxon.assembly_gaps

888888888888888888888888888888888888888888888888888888888888888888888

HEADLINE

my ($curr_dir) = $FindBin::Bin;
$curr_dir =~ s/(.*)\//$1/;

#-----------------------------------------------------------------------------------------
#	Get DB parameters
#
use db_parameters;
use ensembl_parameters;
my %taxon_data = get_taxon_data();
#
#	Get DB parameters
#-----------------------------------------------------------------------------------------

my $dbh = connect_to_panda();

#
#	Download peptide and cDNA files if necessary
#
$dbh->do("TRUNCATE taxon.assembly_gaps;");
$dbh->do("VACUUM taxon.assembly_gaps;");
for my $taxon(keys %taxon_data)
{
	my $seq_dir = $taxon_data{$taxon}[SEQ_DIR];
	if (-f "$seq_dir/gaps.list")
	{

		print STDERR "\tCopying $taxon assembly gaps to panda\n";
		open GAPS, "$seq_dir/gaps.list" or die;
		$dbh->do("COPY taxon.assembly_gaps(species, chromosome, start, finish, type, bridge) FROM STDIN");
		my $ignore = <GAPS>;
		for (<GAPS>)
		{
			chomp;
			my (undef, $chrm, $start, $finish, undef, undef, undef, $type, $bridge) = split /\t/;
			$chrm = substr($chrm, 3);
			$dbh->func("$taxon\t$chrm\t$start\t$finish\t$type\t$bridge\n", 'putline');
		}
		$dbh->func("\\.\n", 'putline');
		$dbh->func('endcopy');

	}
}
$dbh->do("ANALYSE taxon.assembly_gaps;");



$dbh->disconnect;
#_________________________________________________________________________________________

#	Cleanup and finish

#_________________________________________________________________________________________
#unlink glob("$curr_dir/*.data");
#print STDERR "\n";
